#include <stdio.h>  
#include <fcntl.h>  
#include <unistd.h>  


/*
 * NV21转rgb,那么NV12转rgb怎么写呢？很简单，将u、v两句代码互换就可以了。
 */

void NV21_T_RGB(unsigned int width , unsigned int height , unsigned char *yuyv , unsigned char *rgb)  
{  
const int nv_start = width * height ;  
    unsigned int  i, j, index = 0, rgb_index = 0;  
    unsigned char y, u, v;  
    int r, g, b, nv_index = 0;  
      
   
    for(i = 0; i <  height ; i++)  
    {  
        for(j = 0; j < width; j ++){  
            //nv_index = (rgb_index / 2 - width / 2 * ((i + 1) / 2)) * 2;  
            nv_index = i / 2  * width + j - j % 2;  
  
            y = yuyv[rgb_index];  
            u = yuyv[nv_start + nv_index ];  
            v = yuyv[nv_start + nv_index + 1];  
              
          
            r = y + (140 * (v-128))/100;  //r  
            g = y - (34 * (u-128))/100 - (71 * (v-128))/100; //g  
            b = y + (177 * (u-128))/100; //b  
                  
            if(r > 255)   r = 255;  
            if(g > 255)   g = 255;  
            if(b > 255)   b = 255;  
            if(r < 0)     r = 0;  
            if(g < 0)     g = 0;  
            if(b < 0)     b = 0;  
              
            index = rgb_index % width + (height - i - 1) * width;  
            rgb[index * 3+0] = b;  
            rgb[index * 3+1] = g;  
            rgb[index * 3+2] = r;  
            rgb_index++;  
        }  
    }  
}  

int main()
{
	unsigned char yuyv[1080];
	unsigned char rgb[1080];

	NV21_T_RGB(1080, 760, yuyv, rgb);

	return 0;
}
